package net.zhangdaqian.lightDom;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author SunIt
 *
 */
public class Element implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3256906170811957856L;
	private ElementsCollect elementsCollect;
	private String tagName;
	private String id;
	private String name;
	private StringBuffer content = new StringBuffer();
	private Integer level = null;
	private Integer innerId = null;
	private Element parent = null;
	private Boolean endTag = false;
	private LinkedList<Element> children = new LinkedList<Element>();
	private LinkedList<String> attr_keys = null;
	private LinkedList<String> attr_values = null;

	public Integer i_freeToUse = 0;
	public String s_FreeToUse;

	public ElementsCollect getElementsCollect() {
		return elementsCollect;
	}

	public Boolean isEndTag() {
		return endTag;
	}

	public void setEndTag(Boolean endMarks) {
		this.endTag = endMarks;
	}

	public void setElementsCollect(ElementsCollect elementsCollect) {
		this.elementsCollect = elementsCollect;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public StringBuffer getContent() {
		return content;
	}

	public void setContent(StringBuffer content) {
		this.content.append(content);
	}

	public void setContent(String content) {
		this.content.append(content);
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Integer getInnerId() {
		return innerId;
	}

	public void setInnerId(Integer innerId) {
		this.innerId = innerId;
	}

	public Element getParent() {
		return parent;
	}

	public void setParent(Element parent) {
		this.parent = parent;
	}

	public void setChildren(LinkedList<Element> children) {
		this.children = children;
	}

	public LinkedList<Element> getChildren() {
		return children;
	}

	public void setChildren(Element children) {
		this.children.add(children);
	}

	public StringBuffer innerText() {
		StringBuffer text = new StringBuffer();
		if (!tagName.equals("annotation")) {
			text.append(content);
			for (Iterator<Element> iterator = children.iterator(); iterator
					.hasNext();) {
				Element element = iterator.next();
				text.append(element.innerText());
			}
		}
		return text;

	}

	public StringBuffer innerHtml() {
		StringBuffer text = new StringBuffer();
		if (tagName.equals("annotation")) {
			text.append("<!--" + content + "-->");
		} else {
			text.append("<" + tagName + attributesToString() + ">" + content);
			for (Iterator<Element> iterator = children.iterator(); iterator
					.hasNext();) {
				Element element = iterator.next();
				text.append(element.innerHtml());
			}
			if (getElementsCollect().getDocument().getJunitTest()) {
				text.append("</" + tagName + ">");
			} else {
				text.append("</" + tagName + ">" + "\n");
			}
		}
		return text;
	}

	private String attributesToString() {
		if (attr_keys != null) {
			StringBuffer result = new StringBuffer();
			for (int i = 0; i < attr_keys.size(); i++) {
				result.append(" " + attr_keys.get(i) + "=\""
						+ attr_values.get(i) + "\"");
			}
			return result.toString();
		} else {
			return "";
		}
	}

	public ElementsCollect getChildNodes() {
		ElementsCollect elementsCollect = new ElementsCollect();
		for (Iterator<Element> iterator = children.iterator(); iterator
				.hasNext();) {
			Element element = iterator.next();
			elementsCollect.add(element);
		}
		elementsCollect.sortByInnerId();
		return elementsCollect;
	}

	public Element getFirstChild() {
		return children.getFirst();
	}

	public Element getLastChild() {
		return children.getLast();
	}

	public void setAttribute(String key, String value) {
		if (attr_keys == null) {
			attr_keys = new LinkedList<String>();
		}
		if (attr_values == null) {
			attr_values = new LinkedList<String>();
		}
		int i = attr_keys.indexOf(key);
		if (i >= 0) {
			attr_values.set(i, value);
		} else {
			attr_keys.add(key);
			attr_values.add(value);
		}
	}

	public void setAttribute(LinkedList<String> keyList,
			LinkedList<String> valueList) {
		if (attr_keys == null) {
			attr_keys = new LinkedList<String>();
		}
		if (attr_values == null) {
			attr_values = new LinkedList<String>();
		}
		for (int i = 0; i < keyList.size(); i++) {
			attr_keys.add(keyList.get(i));
			attr_values.add(valueList.get(i));
		}
	}

	public String getAttribute(String key) {
		if (attr_keys != null) {
			int i = attr_keys.indexOf(key);
			if (i >= 0) {
				return attr_values.get(i);
			}
		}
		return "";
	}

	public String getCurrentStyleProperty(String key) {
		int i = attr_keys.indexOf("style");
		String css = null;
		if (i >= 0) {
			css = attr_values.get(i);

		} else {
			return "";
		}
		return css;
	}

	public String toString() {
		StringBuffer result = new StringBuffer();
		result.append("----------------------------------------------" + " \n");
		result.append("innerId:" + innerId + " \n");
		result.append("tagName:" + tagName + " \n");
		result.append("content:" + content + " \n");
		result.append("id:" + id + " \n");
		result.append("name:" + name + " \n");
		result.append("attributes:" + attributesToString() + " \n");

		result.append("level:" + level + " \n");

		result.append("children:" + children.size() + " \n");
		result.append("----------------------------------------------" + " \n");
		return result.toString();
	}

}
